package com.example.fanday.play.mvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.fanday.play.base.BaseApplication;
import com.example.fanday.play.di.component.ActivityComponent;
import com.example.fanday.play.di.component.DaggerActivityComponent;
import com.example.fanday.play.di.module.ActivityModule;

/**
 * Created by fanday on 2017/8/13.
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected T presenter;
    protected ActivityComponent activityComponent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivityComponent();
        presenter = initInject();
        presenter.attachView(this);
    }

    protected abstract T initInject();

    private void initActivityComponent() {
        activityComponent = DaggerActivityComponent.builder().
                applicationComponent(((BaseApplication) getApplication()).
                        getApplicationComponent()).activityModule(new ActivityModule(this))
                .build();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
